<template>
  <div>
    <el-button
        type="primary"
        icon="el-icon-edit"
        @click="dialogVisible = true"
        size="mini"
    ></el-button>
    <!-- 编辑用户对话框 -->
    <el-dialog
        title="编辑用户"
        :visible.sync="dialogVisible"
        width="50%"
        :destroy-on-close="destroyonclose"
        @open="getSendTypeList();getOrderinfo()"
    >
      <el-form :model="orderInfo" :rules="rules" ref="orderInfo" :label-width="formLabelWidth">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="orderInfo.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话号码" prop="phone">
          <el-input v-model="orderInfo.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="orderInfo.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="送货方式" prop="sendType">
          <el-select v-model="orderInfo.sendType.id" @change="selectChange" placeholder="请选择送货方式">
            <el-option v-for="sendType in sendTypeList" :key="sendType.id" :label="sendType.name"
                       :value="sendType.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="订单备注">
          <el-input v-model="orderInfo.remarks" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('orderInfo')">保 存</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    orderId: String
  },
  data() {
    return {
      sendTypeList:[],
      status_icon: true,
      dialogVisible: false,
      destroyonclose: true,
      formLabelWidth: '120px',
      orderInfo: {
        name: '',
        phone: '',
        address: '',
        remarks: '',
        sendType: {
          id:''
        },
      },
      rules: {
        name: [
          { required: true, message: '请输入收货人姓名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        phone: [
          {required: true,message: '请输入电话号码', trigger: 'blur'},
          {
            pattern: /^1[3|4|5|6|7|8][0-9]\d{8}$/,
            message: "手机号格式不对",
            trigger: "blur",
          },
        ],
        address: [
          { required: true, message: '输入收货地址', trigger: 'change' },
          {min: 3, max: 20, message: '请正确输入收获地址', trigger: 'blur'}
        ],
        sendType:[
          {required: true, message: '请选择送货方式', trigger: 'change'}
        ]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.axios.post('/admin/order/update', this.orderInfo)
              .then((res) => {
                if (res.data.code == 1) {
                  this.$notify.success({
                    title: "修改成功",
                    message: "已成功保存了一条订单信息！"
                  })
                  this.dialogVisible = false
                  this.$emit("editSuccess")
                } else {
                  this.$message.error(res.data.msg)
                }
              })
        } else {
          this.$message.error('请检查所填写的订单信息！')
          return false;
        }
      });
    },
    async getOrderinfo() {
      this.axios.get("/admin/order/get/" + this.orderId)
          .then(res => {
            if (res.data.code == 1){
              this.orderInfo = res.data.data
            }else{
              this.$message.error("获取订单信息失败！")
            }
          })
    },
    selectChange() {
      this.$forceUpdate()
    },
    getSendTypeList(){
      this.axios.get("/sendType/getSendTypeEnabled")
          .then(res => {
            if (res.data.code == 1){
              this.sendTypeList = res.data.data
            }else{
              this.$notify.error({
                title: '失败',
                message: '获取送货方式失败！'
              })
            }
          })
    }
  }
}
</script>

<style scoped>

</style>